package com.vanzo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.vanzo.Tpplication;
import com.vanzo.encryption.R;

import java.security.PrivateKey;
import java.util.List;

/**
 * Created by tisen on 2018/3/9.
 */

public class ChannelAdapter extends RecyclerView.Adapter<ChannelAdapter.ChannelView> {

    private Context context;
    private List<Tpplication.Channel> channels;
    private int selectPosition;
    private boolean isLocked;

    public ChannelAdapter(Context context) {
        this.context = context;
    }

    public void setChannels(List<Tpplication.Channel> channels) {
        this.channels = channels;
        notifyDataSetChanged();
    }

    public void setSelect(int select) {
        selectPosition = select;
        notifyDataSetChanged();
    }

    @Override
    public ChannelView onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ChannelView(LayoutInflater.from(parent.getContext()).inflate(R.layout.channel_item_view, parent, false));
    }

    @Override
    public void onBindViewHolder(ChannelView holder, int position) {
        holder.setChannel(channels.get(position), position);
    }

    @Override
    public int getItemCount() {
        return channels.size();
    }

    public void setLocked(boolean locked) {
        this.isLocked = locked;
    }

    public class ChannelView extends RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView channelId, channelNum, channelTFreq, channelRFreq;
        private ImageView select;
        private int position;

        public ChannelView(View itemView) {
            super(itemView);
            channelId = (TextView) itemView.findViewById(R.id.channel_id);
            channelNum = (TextView) itemView.findViewById(R.id.channel_num);
            channelTFreq = (TextView) itemView.findViewById(R.id.channel_tfreq);
            channelRFreq = (TextView) itemView.findViewById(R.id.channel_rfreq);
            select = (ImageView) itemView.findViewById(R.id.channel_select);
            itemView.setOnClickListener(this);
        }

        public void setChannel(Tpplication.Channel channel, int position) {
            this.position = position;
            channelId.setText("频道Id:" + channel.getId());
            channelNum.setText("信道:" + channel.getChannel());
            channelTFreq.setText("发射频率:" + channel.getTfreq());
            channelRFreq.setText("接收频率:" + channel.getRfreq());
            select.setVisibility(channel.getId() == selectPosition ? View.VISIBLE : View.INVISIBLE);
        }

        @Override
        public void onClick(View v) {
            if (isLocked) return;
            listener.onChannelSelect(position);
        }
    }

    private ChannelSelectListener listener;

    public void setListener(ChannelSelectListener listener) {
        this.listener = listener;
    }

    public interface ChannelSelectListener {
        void onChannelSelect(int position);
    }
}
